import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * UserGroupManagement 用户组管理
 * @date: 2019-1-14


 */
import { createPagination, getResponse, parseParameters } from 'utils/utils';
import { assignUsersToGroup as _assignUsersToGroup, createUserGroup as _createUserGroup, delCurrentGroupUsers as _delCurrentGroupUsers, deleteUserGroup as _deleteUserGroup, fetchUserGroupList as _fetchUserGroupList, getCurrentGroupUsers as _getCurrentGroupUsers, getCurrentRestGroupUsers as _getCurrentRestGroupUsers, getUserGroupDetail as _getUserGroupDetail, updateUserGroup as _updateUserGroup } from "../services/userGroupManagementService";
export default {
  namespace: 'userGroupManagement',
  state: {
    userGroupList: [],
    // 用户组列表
    pagination: {},
    // 分页对象
    userGroupDetail: {} // 查询列表

  },
  effects: {
    // 获取用户组列表
    fetchUserGroupList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchUserGroupList(_ref, _ref2) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function fetchUserGroupList$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.next = 4;
              return call(_fetchUserGroupList, parseParameters(payload));

            case 4:
              res = _context.sent;
              list = getResponse(res);

              if (!list) {
                _context.next = 9;
                break;
              }

              _context.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  userGroupList: list.content,
                  pagination: createPagination(list)
                }
              });

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, fetchUserGroupList);
    }),
    // 查询用户组列表
    getUserGroupDetail: /*#__PURE__*/_regeneratorRuntime.mark(function getUserGroupDetail(_ref3, _ref4) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function getUserGroupDetail$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              _context2.next = 4;
              return call(_getUserGroupDetail, payload);

            case 4:
              res = _context2.sent;
              list = getResponse(res);

              if (!list) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  userGroupDetail: list
                }
              });

            case 9:
            case "end":
              return _context2.stop();
          }
        }
      }, getUserGroupDetail);
    }),
    // 新增用户组
    createUserGroup: /*#__PURE__*/_regeneratorRuntime.mark(function createUserGroup(_ref5, _ref6) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createUserGroup$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context3.next = 4;
              return call(_createUserGroup, payload);

            case 4:
              res = _context3.sent;
              return _context3.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, createUserGroup);
    }),
    // 更新用户组
    updateUserGroup: /*#__PURE__*/_regeneratorRuntime.mark(function updateUserGroup(_ref7, _ref8) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateUserGroup$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              _context4.next = 4;
              return call(_updateUserGroup, payload);

            case 4:
              res = _context4.sent;
              return _context4.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, updateUserGroup);
    }),
    // 删除用户组
    deleteUserGroup: /*#__PURE__*/_regeneratorRuntime.mark(function deleteUserGroup(_ref9, _ref10) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function deleteUserGroup$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call;
              _context5.next = 4;
              return call(_deleteUserGroup, payload);

            case 4:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, deleteUserGroup);
    }),
    // 查询 对应用户组 未分配的用户
    getCurrentRestGroupUsers: /*#__PURE__*/_regeneratorRuntime.mark(function getCurrentRestGroupUsers(_ref11, _ref12) {
      var payload, call, userGroupId, query, res;
      return _regeneratorRuntime.wrap(function getCurrentRestGroupUsers$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref11.payload;
              call = _ref12.call;
              userGroupId = payload.userGroupId, query = payload.query;
              _context6.next = 5;
              return call(_getCurrentRestGroupUsers, userGroupId, query);

            case 5:
              res = _context6.sent;
              return _context6.abrupt("return", getResponse(res));

            case 7:
            case "end":
              return _context6.stop();
          }
        }
      }, getCurrentRestGroupUsers);
    }),
    // 给 用户组 分配用户
    assignUsersToGroup: /*#__PURE__*/_regeneratorRuntime.mark(function assignUsersToGroup(_ref13, _ref14) {
      var payload, call, userGroupId, users, tenantId, res;
      return _regeneratorRuntime.wrap(function assignUsersToGroup$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref13.payload;
              call = _ref14.call;
              userGroupId = payload.userGroupId, users = payload.users, tenantId = payload.tenantId;
              _context7.next = 5;
              return call(_assignUsersToGroup, userGroupId, tenantId, users);

            case 5:
              res = _context7.sent;
              return _context7.abrupt("return", getResponse(res));

            case 7:
            case "end":
              return _context7.stop();
          }
        }
      }, assignUsersToGroup);
    }),
    // 删除用户组已分配的用户
    delCurrentGroupUsers: /*#__PURE__*/_regeneratorRuntime.mark(function delCurrentGroupUsers(_ref15, _ref16) {
      var payload, call, userGroupId, users, res;
      return _regeneratorRuntime.wrap(function delCurrentGroupUsers$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref15.payload;
              call = _ref16.call;
              userGroupId = payload.userGroupId, users = payload.users;
              _context8.next = 5;
              return call(_delCurrentGroupUsers, userGroupId, users);

            case 5:
              res = _context8.sent;
              return _context8.abrupt("return", getResponse(res));

            case 7:
            case "end":
              return _context8.stop();
          }
        }
      }, delCurrentGroupUsers);
    }),
    // 查询用户组已分配的用户
    getCurrentGroupUsers: /*#__PURE__*/_regeneratorRuntime.mark(function getCurrentGroupUsers(_ref17, _ref18) {
      var payload, call, userGroupId, query, res;
      return _regeneratorRuntime.wrap(function getCurrentGroupUsers$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              payload = _ref17.payload;
              call = _ref18.call;
              userGroupId = payload.userGroupId, query = payload.query;
              _context9.next = 5;
              return call(_getCurrentGroupUsers, userGroupId, query);

            case 5:
              res = _context9.sent;
              return _context9.abrupt("return", getResponse(res));

            case 7:
            case "end":
              return _context9.stop();
          }
        }
      }, getCurrentGroupUsers);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref19) {
      var payload = _ref19.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};